package com.example.zwh.computershop.classify;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.sqlModel.Product;

import java.util.ArrayList;


public class Classify_Fragment extends Fragment implements ClassifyBtnAdapter.OnItemClickLitener{

    private Toast toast;
    private Myapp myapp;

    private View view;
    private RecyclerView btnRecyclerView;
    private ClassifyBtnAdapter classifyBtnAdapter;

    private RecyclerView showRecyclerView;
    private ClassifyShowAdapter classifyShowAdapter;

    private ArrayList<Classify_btn> btnList = new ArrayList<>();;

    private ArrayList<Product> showListT = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_classify, container, false);


        initView();
        ininTest();
        initEvent();
        initData();
        return view;
    }
    //测试数据
    void ininTest() {
        btnList.clear();
        showListT.clear();

        Classify_btn b1 = new Classify_btn("联想", 1);
        btnList.add(b1);
        Classify_btn b2 = new Classify_btn("华硕", 2);
        btnList.add(b2);
        Classify_btn b3 = new Classify_btn("戴尔", 3);
        btnList.add(b3);
        Classify_btn b4 = new Classify_btn("惠普", 4);
        btnList.add(b4);

        getMarkPro(1, showListT, myapp.getProducts());
//        Product p1 = new Product();
//        p1.setPrice(1000);
//        p1.setProductInfor("testsetsetstsetsetsetset");
//        showListT.add(p1);
    }

    void initView() {

        myapp = (Myapp) getActivity().getApplication();
        //分类左边按钮
        btnRecyclerView = view.findViewById(R.id.classify_btn);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        btnRecyclerView.setLayoutManager(layoutManager);
        classifyBtnAdapter = new ClassifyBtnAdapter(btnList);
        btnRecyclerView.setAdapter(classifyBtnAdapter);
        //分类右边显示
        showRecyclerView = view.findViewById(R.id.classify_show);
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getActivity());
        showRecyclerView.setLayoutManager(layoutManager2);
        classifyShowAdapter = new ClassifyShowAdapter(showListT, getContext());
        showRecyclerView.setAdapter(classifyShowAdapter);
    }

    void initEvent() {
        classifyBtnAdapter.setOnItemClickLitener(this);
    }

    void initData() {

    }

    public void onItemClick(String name, int mark) {
        System.out.println("**********mark:" + mark);
        //显示不同种类的商品
        getMarkPro(mark, showListT, myapp.getProducts());

        if (mark == 2) {
            classifyShowAdapter.setmShowList(showListT);
        }else if (mark == 1) {
            classifyShowAdapter.setmShowList(showListT);
        }else if (mark == 3) {
            classifyShowAdapter.setmShowList(showListT);
        }else if (mark == 4) {
            classifyShowAdapter.setmShowList(showListT);
        }
        toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
    }
    public void getMarkPro(int mark, ArrayList<Product> pos, ArrayList<Product> temp) {
        pos.clear();
        for (int i = 0; i < temp.size(); i++) {
            if (temp.get(i).getMark() == mark) {
                pos.add(temp.get(i));
            }
        }
    }
}
